﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    public class AreaLiquidacionRepository: MasterRepository, IAreaLiquidacionREpository
    {
        public List<AREA_LIQUIDACION> GetAllFromArea()
        {
            var query = from p in context.AREA_LIQUIDACIONs
                        select p;

            return query.ToList();
        }

        public List<AREA_LIQUIDACION> GetAllFromAreaByDescripcion(string descripcion)
        {
            if (descripcion != string.Empty)
            {
                var query = from p in context.AREA_LIQUIDACIONs
                            where p.Descripcion.ToUpper().Contains(descripcion.ToUpper())
                            select p;

                return query.ToList();

            }
            else
            {
                var query = from p in context.AREA_LIQUIDACIONs
                            select p;

                return query.ToList();
            }
        }

        public AREA_LIQUIDACION GetFromAreaById(int id)
        {
            var query = from p in context.AREA_LIQUIDACIONs
                        where p.IdAreaLiquidacion.Equals(id)
                        select p;

            return query.SingleOrDefault();
        }

        public void AddArea(AREA_LIQUIDACION area)
        {
            context.AREA_LIQUIDACIONs.Add(area);
            context.SaveChanges();
        }

        public void UpdateArea(AREA_LIQUIDACION area)
        {
            context.Entry(area).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteArea(int id)
        {
            var existe = context.AREA_LIQUIDACIONs.Where(p => p.IdAreaLiquidacion.Equals(id)).SingleOrDefault();

            if (existe != null)
            {
                context.AREA_LIQUIDACIONs.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool ExisteDuplicidad(string descripcion, int id)
        {
            var query = from p in context.AREA_LIQUIDACIONs
                        where p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        && p.IdAreaLiquidacion != id
                        select p;

            return query.Any();
        }
    }
}
